package com.cc.pokergame.model;

import androidx.annotation.NonNull;

import com.cc.pokergame.R;
import com.cc.pokergame.utils.Constants;

/**
 * 定义扑克类
 */
public class Poker {
    // 花色
    private int suit;
    // 点数
    private int point;
    // 默认提供牌的背面资源
    private int pokerBack = R.drawable.shirt_blue;
    // 设置牌的默认状态
    private PokerState pokerState = PokerState.BACK;
    // 拿到确定的牌
    private int pokerRes;

    public Poker(int suit, int point) {
        this.suit = suit;
        this.point = point;

        // 通过花色和点数确定牌对应的资源
        this.pokerRes = Constants.pokerArray[suit][point];
    }

    // 获取资源
    public int getPokerRes() {
        // 判断牌的状态
        return pokerState == PokerState.BACK ? pokerBack : pokerRes;
    }

    // 修改牌状态
    public void changePokerState(){
        this.pokerState = this.pokerState == PokerState.BACK ? PokerState.FRONT : PokerState.BACK;
    }

    /**
     * 将牌的状态设置为正面朝上
     */
    public void changeToFrontState(){
        this.pokerState = PokerState.FRONT;
    }

    @NonNull
    @Override
    public String toString() {
        return (point + 1) + getSuit();
    }

    private String getSuit() {
        switch (suit) {
            case 0:
                return "♠";
            case 1:
                return "♥";
            case 2:
                return "♣";
            case 3:
                return "♦";
            default:
                return "王";
        }
    }

    /**
     * 牌的比较
     * @param poker 下一个玩家的牌
     * @return 返回比较结果
     */
    public int compareTo(Poker poker) {
        //先判断是否有王的情况
        if (suit == 4 || poker.suit == 4) {
            //至少有一个是王
            if (suit != 4) { //另一张牌是王
                return -1;
            } else if (poker.suit != 4) { //我的是王
                return 1;
            }
        }

        //只适用于没有王
        //先比点数
        if (point > poker.point) {
            return 1;
        } else if (point < poker.point) {
            return -1;
        } else {
            //点数相同，比花色
            //注意我们花色的索引值按照黑0红1梅2方3 递增
            //实际花色大小时 黑>红>梅>方 递减
            if (suit > poker.suit) {
                return -1;
            } else {
                return 1;
            }
        }
    }

    public enum PokerState{
        BACK, // 背面
        FRONT // 正面
    }
}
